// EnumFormatEtc.cpp

#include "StdAfx.h"

#include "EnumFormatEtc.h"

class ATL_NO_VTABLE CEnumFormatEtc :
	public IEnumFORMATETC,
	public CComObjectRootEx<CComMultiThreadModelNoCS>
{
public:
	BEGIN_COM_MAP(CEnumFormatEtc)
	COM_INTERFACE_ENTRY(IEnumFORMATETC)
	END_COM_MAP()



	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}

	void FinalRelease();

	void Init (const FORMATETC* pFormatEtc, ULONG numFormats);

	STDMETHOD(Next)(ULONG celt, FORMATETC* rgelt, ULONG* pceltFetched);
	STDMETHOD(Skip)(ULONG celt);
	STDMETHOD(Reset)(void);
	STDMETHOD(Clone)(IEnumFORMATETC** ppEnumFormatEtc);


private:
	LONG m_RefCount;
	ULONG m_NumFormats;
	FORMATETC* m_Formats;
	ULONG m_Index;
};

static void DeepCopyFormatEtc(FORMATETC* dest, const FORMATETC* src)
{
	*dest = *src;
	if(src->ptd)
	{
		dest->ptd = (DVTARGETDEVICE*)CoTaskMemAlloc(sizeof(DVTARGETDEVICE));
		*(dest->ptd) = *(src->ptd);
	}
}

void CEnumFormatEtc::Init(const FORMATETC* pFormatEtc, ULONG numFormats)
{
	m_RefCount = 1;
	m_Index = 0;
	m_NumFormats = 0;
	m_Formats = new FORMATETC[numFormats];

	{
		m_NumFormats = numFormats;
		for(ULONG i = 0; i < numFormats; i++)
		{
			DeepCopyFormatEtc(&m_Formats[i], &pFormatEtc[i]);
		}
	}
}

void CEnumFormatEtc::FinalRelease()
{
	if(m_Formats)
	{
		for(ULONG i = 0; i < m_NumFormats; i++)
			if(m_Formats[i].ptd)
			{
				CoTaskMemFree(m_Formats[i].ptd);
			}
		delete[]m_Formats;
	}
}

STDMETHODIMP CEnumFormatEtc::Next(ULONG celt, FORMATETC* pFormatEtc, ULONG* pceltFetched)
{
	ULONG copied  = 0;
	if(celt == 0 || pFormatEtc == 0)
	{
		return E_INVALIDARG;
	}
	while(m_Index < m_NumFormats && copied < celt)
	{
		DeepCopyFormatEtc(&pFormatEtc[copied], &m_Formats[m_Index]);
		copied++;
		m_Index++;
	}
	if(pceltFetched != 0)
	{
		*pceltFetched = copied;
	}
	return (copied == celt) ? S_OK : S_FALSE;
}

STDMETHODIMP CEnumFormatEtc::Skip(ULONG celt)
{
	m_Index += celt;
	return (m_Index <= m_NumFormats) ? S_OK : S_FALSE;
}

STDMETHODIMP CEnumFormatEtc::Reset(void)
{
	m_Index = 0;
	return S_OK;
}

STDMETHODIMP CEnumFormatEtc::Clone(IEnumFORMATETC** ppEnumFormatEtc)
{
	HRESULT hResult = CreateEnumFormatEtc(m_NumFormats, m_Formats, ppEnumFormatEtc);
	if(hResult == S_OK)
	{
		((CEnumFormatEtc*)*ppEnumFormatEtc)->m_Index = m_Index;
	}
	return hResult;
}

// replacement for SHCreateStdEnumFmtEtc
HRESULT CreateEnumFormatEtc(UINT numFormats, const FORMATETC* formats, IEnumFORMATETC** enumFormat)
{
	if(numFormats == 0 || formats == 0 || enumFormat == 0)
	{
		return E_INVALIDARG;
	}
	CEnumFormatEtc* nFormats = new CComObject<CEnumFormatEtc>;
	nFormats->FinalConstruct();
	nFormats->Init(formats, numFormats);
	*enumFormat = nFormats;


	return (*enumFormat) ? S_OK : E_OUTOFMEMORY;
}
